package com.moonce.api.controller;

import com.moonce.api.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author 马路遥(229010812 @ qq.com)
 * @version V1.0
 * @Title: springmvc-demo
 * @Package com.moonce.api.controller
 * @Description: User控制层
 * @Date 2017-12-14 11:46
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * @Description: 添加用户
     * @param username  用户名
     * @param password  密码
     * @param name  真实姓名
     * @param email 邮箱
     * @param sex   性别
     * @return
     * @throws Exception
     * @author 马路遥(229010812@qq.com)
     * @Date 2017/12/14 11:53
     */
    @ResponseBody
    @RequestMapping(value = "add",method =RequestMethod.POST)
    public String add(@RequestParam String username,
                      @RequestParam String password,
                      @RequestParam(required = false) String name,
                      @RequestParam(required = false) String email,
                      @RequestParam(required = false) byte sex
    ){

        return userService.add(username,password,name,email,sex);
    }

    /**
     * @Description: 修改用户
     * @param id    用户编号
     * @param username  用户名
     * @param password  密码
     * @param name  真实姓名
     * @param email 邮箱
     * @param sex   性别
     * @return
     * @throws Exception
     * @author 马路遥(229010812@qq.com)
     * @Date 2017/12/14 11:57
     */
    @ResponseBody
    @RequestMapping(value = "update",method =RequestMethod.POST)
    public String update(@RequestParam Integer id,
                         @RequestParam(required = false) String username,
                         @RequestParam(required = false) String password,
                         @RequestParam(required = false) String name,
                         @RequestParam(required = false) String email,
                         @RequestParam(required = false) byte sex){

        return userService.update(id,username,password,name,email,sex);
    }

    /**
     * @Description: 分页查询用户信息列表
     * @param id    用户编号
     * @param username  用户名
     * @param email 邮箱
     * @return
     * @throws Exception
     * @author 马路遥(229010812@qq.com)
     * @Date 2017/12/14 11:57
     */
    @ResponseBody
    @RequestMapping(value = "list",method =RequestMethod.GET)
    public String list(@RequestParam(required = false) Integer id,
                       @RequestParam(required = false) String username,
                       @RequestParam(required = false) String email,
                       @RequestParam(required = false) Integer pageNo ,
                       @RequestParam(required = false) Integer pageSize){
        return userService.list(id,username,email,pageNo,pageSize);
    }

    /**
     * @Description: 删除用户
     * @param id    用户编号
     * @return
     * @throws Exception
     * @author 马路遥(229010812@qq.com)
     * @Date 2017/12/14 11:57
     */
    @ResponseBody
    @RequestMapping(value = "delete",method =RequestMethod.GET)
    public String delete(@RequestParam(required = false) Integer id){
        return userService.delete(id);
    }

    /**
     * @Description: 用户登录
     * @param username  用户名
     * @param password  密码
     * @return
     * @throws Exception
     * @author 马路遥(229010812@qq.com)
     * @Date 2017/12/14 11:57
     */
    @ResponseBody
    @RequestMapping(value = "sign-in",method =RequestMethod.POST)
    public String sigIn(@RequestParam String username,
                        @RequestParam String password){

        return userService.sigIn(username,password);
    }
}
